package com.yuerlive.rainbow.module.login;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.API;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.common.Constants;
import com.yuerlive.rainbow.common.SimpleTextWatcher;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.module.login.presenter.LoginContract;
import com.yuerlive.rainbow.module.login.presenter.LoginPresenter;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.ToastUtils;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.widget.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Admin on 2017/6/5.
 */

public class BindMobileActivity extends BaseActivity implements LoginContract.View {
    @BindView(R.id.llParent)
    LinearLayout llParent;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLogintMobile)
    EditText etLogintMobile;
    @BindView(R.id.etLogintVerification)
    EditText etLogintVerification;
    @BindView(R.id.tvGetVerification)
    TextView tvGetVerification;
    @BindView(R.id.tvCompeleted)
    TextView tvCompeleted;
    @BindView(R.id.tvProtocol)
    TextView tvProtocol;
    @BindView(R.id.llThirdLogin)
    LinearLayout llThirdLogin;
    private CustomDialog dialog;
    private String imgCode;

    @BindView(R.id.ivClear)
            ImageView ivClear;
    LoginContract.Presenter presenter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        setTitle("绑定手机号");
        new LoginPresenter(this);
        tvTitle.setVisibility(View.GONE);
        tvProtocol.setVisibility(View.GONE);
        llThirdLogin.setVisibility(View.GONE);
        etLogintMobile.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s.length() == 11) {
                    tvGetVerification.setTextColor(getResources().getColor(R.color.input_red));
                } else {
                    tvGetVerification.setTextColor(getResources().getColor(R.color.input_red_60));
                }
                if (s.length()>0){
                    ivClear.setVisibility(View.VISIBLE);
                }else {
                    ivClear.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.tvGetVerification, R.id.tvCompeleted,R.id.ivClear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvGetVerification:
                if (checkoutPhone(etLogintMobile.getText().toString())) {
                    showInputVerificationDialog();
                } else {
                    // showToast("请输入正确的手机号码");
                    showSnackbar(llParent, "请输入正确的手机号码", 0);
                }
                break;
            case R.id.tvCompeleted:
                // jumpToActivity(CompleteInfoActivity.class);
                presenter.getEditRequest(etLogintMobile.getText().toString(), etLogintVerification.getText().toString());
                break;
            case R.id.ivClear:
                etLogintMobile.setText("");
                break;
        }
    }

    private boolean checkoutPhone(String phoneNum) {
        return phoneNum.matches(Constants.PHONE_FORMAT);
    }

    /**
     *
     *
     */
    private void showInputVerificationDialog() {
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_edit_verification, null);
        dialog = new CustomDialog(mActivity, contentView) {
            @Override
            public void dismiss() {
                super.dismiss();
                imgCode = "";
            }
        };
        RelativeLayout rlInput = (RelativeLayout) contentView.findViewById(R.id.rlInput);
        TextView tvTitle = (TextView) contentView.findViewById(R.id.tvTitle);
        final ImageView ivClear = (ImageView) contentView.findViewById(R.id.ivClear);
        final EditText etInput = (EditText) contentView.findViewById(R.id.etInput);
        TextView tvCancle = (TextView) contentView.findViewById(R.id.tvCancle);
        TextView tvSure = (TextView) contentView.findViewById(R.id.tvSure);
        ImageView ivDelete = (ImageView) contentView.findViewById(R.id.ivDelete);
        ImageView ivVerificationPic = (ImageView) contentView.findViewById(R.id.ivVerificationPic);
        GlideUtil.loadNoCache(mActivity, API.CHECK_CODE + etLogintMobile.getText().toString(), ivVerificationPic);
        ivClear.setVisibility(View.GONE);
        tvTitle.setText(getResources().getString(R.string.please_input_pic_content));
        ivClear.setVisibility(View.GONE);
        popupInputMethodWindow();
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etInput.setText("");
                imgCode = "";
            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Todo 网络请求修改名字
                if (!checkoutInputCode()) {
                    showSnackbar(llParent, "验证码不正确，请确认后重试", 0);
                    return;
                }
                imgCode = etInput.getText().toString();
                dialog.dismiss();
                imgCode = "";
                presenter.getSmsCode(etLogintMobile.getText().toString(), etInput.getText().toString(),2);
            }

            private boolean checkoutInputCode() {
                return etInput.getText().toString().trim().length() == 4;
            }
        });
    }

    private void popupInputMethodWindow() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) BindMobileActivity.this.getSystemService(Service.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 0);
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        super.presenter = this.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showImgCode(String imgUrl) {

    }

    @Override
    public void loginResult(User user) {
    }

    @Override
    public void sendSuccess(BaseData baseData) {
        switch (baseData.getCode()) {
            case 0:
                showToast("发送成功");
                break;
            default:
                showSnackbar(getRootView(mActivity),baseData.getResult(),0);
        }
    }

    @Override
    public void bindMobileSuccess(BaseData baseData) {
        if (baseData.getCode() == 0) {
            showSnackbar(getRootView(mActivity),"绑定手机号成功",0);
            UserProxy.getUser().getUserInfo().setMobile(etLogintMobile.getText().toString());
            Intent intent = new Intent();
            intent.putExtra("mobile", etLogintMobile.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }else {
            showSnackbar(getRootView(mActivity),baseData.getResult(),0);
        }
    }

    @Override
    public void showFail(String message, String api) {

    }

    @Override
    public void loginFaild(int code, String result) {

    }
}
